import { defHttp } from '@/utils/http/axios';
import { useMessage } from '@/hooks/web/useMessage';
import { Iresult } from '#/axios';

const { createMessage } = useMessage();

enum Api {
  url = 'Menus',
  urlList = 'Roles/RoleMenuTrees'
}

/**
 * 列表接口
 * @param params
 */
export const list = (params) => {
  // return defHttp.get({ url: Api.list, params })
  return new Promise((resolve) => {
    defHttp.get<Iresult>({ url: Api.urlList, params }).then((res) => {
      resolve({ data: res.data });
    });
  });
};
export const listMunu = (params) => {
  // return defHttp.get({ url: Api.list, params })
  return new Promise((resolve) => {
    defHttp.get<Iresult>({ url: Api.url, params }).then((res) => {
      resolve({ data: res.data });
    });
  });
};

/**
 * 删除菜单
 */
export const deleteMenu = (params, handleSuccess) => {
  return defHttp
    .delete({ url: `${Api.url}/${params.id}` }, { joinParamsToUrl: true })
    .then((res) => {
      createMessage.success(res.msg || '删除成功');
      handleSuccess();
    });
};
/**
 * 保存或者更新菜单
 * @param params
 */
export const saveOrUpdateMenu = (params, isUpdate) => {
  const type = isUpdate ? 'put' : 'post';
  const url = isUpdate ? `${Api.url}/${params.id}` : Api.url;
  return defHttp[type]({ url, params });
};
